package com.compass.doctor.dialog

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.FragmentActivity
import com.compass.doctor.callback.OnGiveMessageCallBack
import com.compass.doctor.databinding.DialogPresentedNumberBinding
import com.compass.doctor.ui.patient.viewmodel.ExamineViewModel
import com.compass.framework.base.BaseDialog
import com.compass.framework.base.BaseDialogFragment
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.toast.TipsToast
import com.compass.framework.utils.MoneyInputFilter

/**
 * Description： 赠送条数dialog
 * Date:2023/12/14 20:37
 */
class PresentedNumberDialog {
    class Builder(
        activity: FragmentActivity,
        private var onGiveMessageCallBack: OnGiveMessageCallBack
    ) : BaseDialogFragment.Builder<Builder>(activity) {


        private val mBinding: DialogPresentedNumberBinding =
            DialogPresentedNumberBinding.inflate(LayoutInflater.from(activity))


        init {
            initView()
        }

        private fun initView() {
            setContentView(mBinding.root)
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
            gravity = Gravity.BOTTOM
            showGift()

            mBinding.edtNumber.addTextChangedListener {
                if (it.toString().isEmpty()) mBinding.edtNumber.textSize =
                    14f else mBinding.edtNumber.textSize = 18f
            }

            mBinding.tvGiveTime.addTextChangedListener {
                if (it.toString().isEmpty()) mBinding.tvGiveTime.textSize =
                    14f else mBinding.tvGiveTime.textSize = 18f
            }

            mBinding.tvCancel.onClick {
                dismiss()
            }
            mBinding.tvRefusal.onClick {
                val giveNumber = mBinding.edtNumber.text.toString().trim()
                val giveTime = mBinding.tvGiveTime.text.toString().trim()

                if (giveNumber.isEmpty()||giveNumber.toInt()==0){
                    TipsToast.showTips("请先输入赠送条数")
                    return@onClick
                }

                if (giveTime.isEmpty()||giveTime.toInt()==0){
                    TipsToast.showTips("请先输入有效时长")
                    return@onClick
                }
                onGiveMessageCallBack.onMessageCallBack(giveNumber, giveTime)
                dismiss()
            }
        }


        /**
         * 显示赠送按钮
         */
        private fun showGift() {
            val appHospitalConfig=   UserInfoManager.getUserInfo()?.appHospitalConfigRespVO
            appHospitalConfig?.let {

                val filter = MoneyInputFilter()
                filter.setMaxValue(it.giveRow.toDouble())
                filter.setDecimalLength(0)
                mBinding.edtNumber.filters = arrayOf(filter)

                val filterTime = MoneyInputFilter()
                filterTime.setMaxValue(it.giveTime.toDouble())
                filterTime.setDecimalLength(0)
                mBinding.tvGiveTime.filters = arrayOf(filterTime)

                mBinding.edtNumber.setHint("请输入赠送条数(最多${it.giveRow}条)")
                mBinding.tvGiveTime.setHint("请输入赠送有效时长(${it.giveTime}小时内)")

            }
        }



    }
}